<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pendaftaran extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_pendaftaran');
		//$this->load->model('mdl_fakultas');
		//$this->load->model('mdl_prodi');
	}
	
	function index(){
		$this->load->view('pmb/pendaftaran');
	}
	
	
	function grid(){
		$data = $this->mdl_pendaftaran->getdata();
		echo $this->mdl_pendaftaran->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['id_pendaftaran'] = '';
		$data['PMBPeriodID'] = '';
		$data['no_pendaftaran'] = '';
		$data['nama'] = '';
		$data['tempat_lahir'] = '';
		$data['tgl_lahir'] = date('d/m/Y');
		$data['kd_prov'] = '';
		$data['kd_kabup'] = '';
		//$data['nama'] = '';
		$data['alamat'] = '';
		$data['asal_sekolah'] = '';
		$data['no_telp'] = '';
		$data['email'] = '';
		$data['fakultas1'] = '';
		$data['prodi1'] = '';
		$data['fakultas2'] = '';
		$data['prodi2'] = '';
		$data['nama_ortu'] = '';
		$data['alamat_ortu'] = '';
		$data['pekerjaan'] = '';
		$data['status'] = '';
		$this->load->view('pmb/pendaftaran_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_pendaftaran->getdataedit($kode);
		
		$data['id_pendaftaran'] = $kode;
		$data['PMBPeriodID'] = $r->row()->PMBPeriodID;
		$data['no_pendaftaran'] = $r->row()->no_pendaftaran;
		$data['nama'] = $r->row()->nama;
		$data['tempat_lahir'] = $r->row()->tempat_lahir;
		$data['tgl_lahir'] = FormatDateFromMysql($r->row()->tgl_lahir);
		$data['alamat'] = $r->row()->alamat;
		$data['asal_sekolah'] = $r->row()->asal_sekolah;
		$data['no_telp'] = $r->row()->no_telp;
		$data['email'] = $r->row()->email;
		$data['fakultas1'] = $r->row()->fakultas1;
		$data['prodi1'] = $r->row()->prodi1;
		$data['fakultas2'] = $r->row()->fakultas2;
		$data['prodi2'] = $r->row()->prodi2;
		$data['nama_ortu'] = $r->row()->nama_ortu;
		$data['alamat_ortu'] = $r->row()->alamat_ortu;
		$data['pekerjaan'] = $r->row()->pekerjaan;
		$data['status'] = $r->row()->status;
		$this->load->view('pmb/pendaftaran_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("PMBPeriodID", 'Periode / Gelombang', 'trim|required|xss_clean');
		$this->form_validation->set_rules("no_pendaftaran", 'No Pendaftaran', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat", 'Alamat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("asal_sekolah", 'Asal sekolah', 'trim|required|xss_clean');
		$this->form_validation->set_rules("no_telp", 'No Telepon', 'trim|required|xss_clean');
		$this->form_validation->set_rules("email", 'Email', 'trim|required|xss_clean');
		$this->form_validation->set_rules("fakultas1", 'Fakultas 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("prodi1", 'Prodi 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("fakultas2", 'Fakultas 1', 'trim|required|xss_clean');
		$this->form_validation->set_rules("prodi2", 'Prodi 2', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_ortu", 'Nama Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat_ortu", 'Alamat Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("pekerjaan", 'Pekerjaan', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_pendaftaran->InsertOnDb($data);
			}else { // edit
				$result=$this->mdl_pendaftaran->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	
	function delete($kode){
		$result = $this->mdl_pendaftaran->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} 
	
	function pdf(){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_pendaftaran->get_pdf();
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}
		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',16);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Data Pendaftaran Mahasiswa Baru';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Pendaftaran.pdf", "I");
	}
	
	function kartu_format($kode){
		 
		 $data['kd_mahasiswa'] = $kode;
		 
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('L', 'A5', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		 //$data['nama'] = 'indriyanto';
		 $data['namaUniv'] = 'UNIVERSITAS SUBANG';
		 $data['alamatUniv'] = 'Jln. Perkutut Kotaraja, Telp.(0967)581562';
		 $data['kotaUniv'] = 'Subang, Jawa Barat';
		 
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_pendaftaran->get_kartu($kode);
		
		 $konten = $this->load->view('pmb/kartu_format',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('exemple00.pdf');
	}
}